using System;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using Cysharp.Threading.Tasks;

public partial class ArchivePanel: UIPanel
{
    public const string path = "Assets/Prefab/ArchivePanel.prefab";

    private List<ArchiveBlank> m_ArchiveBlanks;

    public ArchivePanel() : base(new UIType(path))
    {
        m_ArchiveBlanks = new List<ArchiveBlank>(3);
    }

    public async override void OnEnter()
    {
        base.OnEnter();

        foreach(RectTransform archive in m_ArchiveBlankRectTransform)
        {
            ArchiveBlank newAchiveBlanck = new ArchiveBlank();

            newAchiveBlanck.Bind(archive);

            newAchiveBlanck.Init();

            m_ArchiveBlanks.Add(newAchiveBlanck);

            newAchiveBlanck.AddToggleClickEventCallback();
        }

        float moveDistance = ParentRectTransform.sizeDelta.x / 2 + UIObj.transform.parent.GetComponent<RectTransform>().sizeDelta.x / 2;

        ParentRectTransform.DOAnchorPosX(ParentRectTransform.anchoredPosition.x - moveDistance, 1);

        await UniTask.Delay(TimeSpan.FromSeconds(1));

        AddInputEventCallback();
    }

    public override void OnPause()
    {
        base.OnPause();
    }

    public override void OnResume()
    {
        base.OnResume();
    }

    public async override UniTask OnStop(bool useAnim)
    {
        await base.OnStop(useAnim);

        foreach(var archive in m_ArchiveBlanks)
        {
            archive.RemoveToggleClickEventCallback();
        }

        if (useAnim)
        {
            float moveDistance = ParentRectTransform.sizeDelta.x / 2 + UIObj.transform.parent.GetComponent<RectTransform>().sizeDelta.x / 2;

            ParentRectTransform.DOAnchorPosX(ParentRectTransform.anchoredPosition.x + moveDistance, 1);

            await UniTask.Delay(TimeSpan.FromSeconds(1));
        }

        RemoveInputEventCallback();
    }
    private void AddInputEventCallback()
    {
        m_ConfirmButton.onClick.AddListener(() =>
        {

        });
        m_CancleButton.onClick.AddListener(async() =>
        {
            await PanelManager.Instance.ClosePanel(this);
        });
    }
    private void RemoveInputEventCallback()
    {
        m_ConfirmButton.onClick.RemoveAllListeners();
        m_CancleButton.onClick.RemoveAllListeners();
    }
}
